package com.etc.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.sql.rowset.CachedRowSet;

import com.etc.dao.RoutePlanDao;
import com.etc.entity.Bus;
import com.etc.entity.Transfer;
import com.etc.entity.User;
import com.etc.util.DBUtil;

public class RoutePlanDaoImpl implements RoutePlanDao{
	
	/**
	 * 路线规划
	 * @return
	 */
	@Override
	public HashMap<Integer, List> routePlan(int start,int end) {
		HashMap<Integer, List> map = new HashMap<>();
		List<Integer> list1 = new ArrayList<>();
		List<Transfer> list2 = new ArrayList<>();
		//直达查询
		String sql = "select A.b_id from (select b_id from bussite where s_id = ?) A,(select b_id from bussite where s_id = ?) B where A.b_id = B.b_id";
		CachedRowSet crs = DBUtil.execQuery(sql,start,end);
		try {
			while(crs.next()) {
				int b_id=crs.getInt(1);
				list1.add(b_id);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//换乘一次查询
		if(list1.size()==0) {
			String sql2 = "select C.a,C.b,D.s_name from(select A.b_id a,B.b_id b,A.s_id c from (select distinct b_id,s_id,s_location from bussite where b_id in (select b_id from bussite where s_id=?) and s_location>(select s_location from bussite where s_id=?) order by s_location ) A,(select distinct b_id,s_id,s_location from bussite where b_id in (select b_id from bussite where s_id=?) order by s_location) B where A.s_id= B.s_id) C,site D where C.c=D.s_id";
			CachedRowSet crs2 = DBUtil.execQuery(sql2,start,start,end);
			try {
				while(crs2.next()) {
					int startBus=crs2.getInt(1);
					int endBus=crs2.getInt(2);
					String transferStation=crs2.getString(3);
					
					list2.add(new Transfer(startBus, transferStation, endBus));
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		
		map.put(1, list1);
		map.put(2, list2);
		return map;
	}

}
